/*
 * author      : 吴中勤 
 * date        : 2019/3/11
 * description : TODO
 */

package org.wzq.learn;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.sleuth.instrument.web.TraceWebServletAutoConfiguration;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;


@Component
@Order(TraceWebServletAutoConfiguration.TRACING_FILTER_ORDER - 1)
public class FilterBeforeTrace extends GenericFilterBean {

    private static final Logger logger = LoggerFactory.getLogger(FilterBeforeTrace.class);

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest httpServeletRequest = (HttpServletRequest) request;
        Enumeration<String> headerNames = httpServeletRequest.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            logger.info("header[{}]={}", name, httpServeletRequest.getHeader(name));
        }

        chain.doFilter(request, response);
    }
}